<?php
class serfdom 
{

  function __construct($user,$user_info,$user_id) {
       //print "In constructor\n";
       $this->user = $user;
       $this->user_info = $user_info;
       $this->user_id = $user_id;
   }

        function land_add($user_id,$land_type,$contract_type,$contract_length,$cost,$trees_min,$trees_max,$risk_of_fire)
        {
            global $db;
            $trees = mt_rand($trees_min, $trees_max);
            $sql = "INSERT INTO `user_land` ( `assoc_id` , `land_type` , `contract_type` , `contract_length` , `contract_cost`,`trees`,`risk_of_fire` ) VALUES ('$user_id','$land_type','$contract_type','$contract_length','$cost','$trees','$risk_of_fire')";
            $result = $db->query($sql);
        }
         
        
        function load_land($user_id, $land_id)
        {
            global $db;
            $sql = "SELECT * FROM `user_land` WHERE `assoc_id`='$user_id' AND `id`='$land_id'";
            return $db->return_first_result($sql);
        }
        
        function land_plough($user_id, $land_id,$trees,$effort_required)
        {
            global $db;
            $this->user->effort_check($effort_required,$this->user_info[$this->user_id]['effort']);
            $time = time();
            $trees_ploughed = mt_rand(0, $trees); 
            $trees_ploughed = mt_rand(0, $trees_ploughed);
            $trees_ploughed = mt_rand(0, $trees_ploughed);
            $this->user->get_item('trees',$trees_ploughed,0);
            $sql = "UPDATE `user_land` SET `output_points` = output_points+'1',`plough` = '1' ,`trees` =trees- '$trees_ploughed',`pl_time`='$time' WHERE `id` ='$land_id' AND `assoc_id`='$user_id' LIMIT 1 ";
            if($db->query($sql))
            {
                return $trees_ploughed;
            }
            else
            {
                return false;
            }
        }

        function collect_manure($land_id,$animals,$effort_required)
        {
            global $db;
            $this->user->effort_check($effort_required,$this->user_info[$this->user_id]['effort']);
            $time = time();
            $manure = mt_rand(0, $animals); 
            $manure = mt_rand(0, $manure);
            $manure = mt_rand(0, $manure);
            $this->user->get_item('manure',$manure,0);
            $sql = "UPDATE `user_land` SET `manure_time`='$time' WHERE `id` ='$land_id' AND `assoc_id`='$this->user_id' LIMIT 1 ";
            if($db->query($sql))
            {
                return $manure;
            }
            else
            {
                return false;
            }
        }
        
        
        function land_plant($land_id,$seed,$max)
        {
            global $db;
			$seed_info = $db->return_first_result("SELECT * FROM `user_items` WHERE `assoc_id`='$this->user_id' AND `item`='$seed'");
			if ($seed_info['farm_item_quantity'] > 0)
			{
                $sql_1 = "UPDATE `user_land` SET `planted_on_land` = '$seed',`max_crop`='$max',`day_of_cycle`='0' WHERE `id` ='$land_id' AND `assoc_id`='$this->user_id' LIMIT 1 ";
                $sql_2 = "UPDATE `user_items` SET `quantity` = `quantity`-1 WHERE `item` ='$seed' AND `assoc_id`='$this->user_id' LIMIT 1 ";
                if($db->query($sql_1) && $db->query($sql_2))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
               return false;
            }
        }
        
        
        function harvest($user_id, $land_id,$land_info,$effort_required)
        {
            global $db;
            $this->user->effort_check($effort_required,$this->user_info[$this->user_id]['effort']);
            $time = time();
            $luck_of_day = mt_rand(0, 6); 
            //$this->user->get_item('trees',$trees_ploughed,0);
            //$sql = "UPDATE `user_land` SET `output_points` = output_points+'1',`plough` = '1' ,`trees` =trees- '$trees_ploughed',`pl_time`='$time' WHERE `id` ='$land_id' AND `assoc_id`='$user_id' LIMIT 1 ";

            echo 'harvesting...';
            
            # check if the user owns the land he is trying to harvest
            if ($user_id == $land_info['assoc_id'] && strlen($land_info['planted_on_land']) > 0)
            {
                $cycle = $land_info['day_of_cycle'];
                
                # Calculate the difference in cycle days from perfect to what the user has
                if($cycle > 7)
                {
                    $diff = 7 - $cycle;
                }
                else
                {
                    $diff = $cycle - 7;
                }
                
                $cycle_bonus = $diff * mt_rand(377,413);
                
                $tree_bonus = round($land_info['trees']/mt_rand(8,12));
                
                $total_harvest = $land_info['max_crop'] + $cycle_bonus - $tree_bonus;
                
                if ($total_harvest < 0){ $total_harvest = 0; }
                echo "<br>diff is $diff; cycle_bonus is $cycle_bonus; total harvest = $total_harvest";
                
                $this->user->get_item($land_info['planted_on_land'],$total_harvest,0);
                
                $sql_1 = "UPDATE `user_land` SET `planted_on_land` = '',`max_crop`='',`day_of_cycle`='0' WHERE `id` ='$land_id' AND `assoc_id`='$this->user_id' LIMIT 1 ";
                if($db->query($sql_1))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

}

?>